﻿using AutoMapper;
using AutoMapper.Configuration.Annotations;
using MediatR;
using S.Eartag.API.Application.Command.LamBingCommand;
using S.Eartag.Domain.Model;
using S.Eartag.Infrastructure.Impl;
using S.Eartag.Infrastructure.Interface;

namespace S.Eartag.API.Application.CommandHandlers.LamBingCommandHandler
{
    public class LamBingUpdateCommandHandler : IRequestHandler<LamBingUpdateCommand, int>
    {
        private readonly ILamBingRepository lamBingRepository;
        private readonly IMapper mapper;
        public LamBingUpdateCommandHandler(ILamBingRepository lamBingRepository, IMapper mapper)
        {
            this.lamBingRepository = lamBingRepository;
            this.mapper = mapper;
        }
        public async Task<int> Handle(LamBingUpdateCommand request, CancellationToken cancellationToken)
        {
            var res = lamBingRepository.All().Result.Where(x => x.EarNumber == request.EarNumber).FirstOrDefault();
            if (res != null)
            {
                return -1;
            }
            return await lamBingRepository.Update(mapper.Map<LamBing>(request));
        }
    }
}
